import adminListV from "../view/adminList.ejs";
import adminTable from "../components/AdminTable.ejs"
import axios from "axios";
const addAdminBtnHandler = function(){
	// document.表单的名字.元素的名字
	// console.log(document.adminForm);// 获取到表单元素
	// console.log(document.adminForm.adminName);// 获取到表单中的input元素
	const adminName = document.adminForm.adminName.value;
	const password = document.adminForm.password.value;
	const rePassword = document.adminForm.rePassword.value;
	// 如果管理员账号不匹配正则
	if(!/^[a-z]+$/i.test(adminName)){
		toastr.error('管理员账号必须全部是英文！');
		return;
	}
	if(!/^\w+$/.test(password)){
		toastr.error('密码只支持英文，数字，下划线');
		return;
	}
	if(password !== rePassword){
		toastr.error('两次输入的密码必须一致！');
		return;
	}
	console.log(adminName,password,rePassword)

}
export default () => {
	return async (req, res) => {
		res.render(adminListV());
		document.querySelector("#addAdminBtn").onclick = addAdminBtnHandler;
		// 发送ajax请求
		const {data} = await axios.get("/api/adminList");
		console.log(data);
		document.querySelector("#adminTable").innerHTML = adminTable(data);
	}
}